package com.nmgs.util;

import com.trkf.PasswordEncryption.PassWordUtils;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Data;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class JDBCUtil {
    public static HikariDataSource getHikariDataSource(String dbName) {
        String DBUserName = PropertiesUtil.getValue(dbName + "DBUserName");
        String DBPassWord = PropertiesUtil.getValue(dbName + "DBPassWord");
        String DBType = PropertiesUtil.getValue(dbName + "DBType");
        String DBUrl = PropertiesUtil.getValue(dbName + "DBUrl");
        if (DBUserName != null && DBUserName.length() > 0 &&
                DBPassWord != null && DBPassWord.length() > 0 &&
                DBType != null && DBType.length() > 0 &&
                DBUrl != null && DBUrl.length() > 0) {
            HikariConfig config = new HikariConfig();
            config.setUsername(DBUserName);
            config.setPassword(PassWordUtils.decrypt(DBPassWord));
            config.setPoolName(dbName);
            config.setMaximumPoolSize(100);
            config.setIdleTimeout(60000);
            config.setMaxLifetime(180000);
            config.setMinimumIdle(1);
            config.setConnectionTimeout(20000);
            config.setConnectionTestQuery(getTestQuery(DBType));
            config.setDriverClassName(getDriverClassName(DBType));
            config.setJdbcUrl(DBUrl);
            return new HikariDataSource(config);
        } else {
            return null;
        }
    }

    public static HikariDataSource getSqliteSource(String name) {
        String DBUrl = "jdbc:sqlite::resource:sqlite/"+name+".sqlite";
        HikariConfig config = new HikariConfig();
        config.setPoolName("sqlite");
        config.setMaximumPoolSize(300);
        config.setIdleTimeout(60000);
        config.setMaxLifetime(180000);
        config.setMinimumIdle(1);
        config.setConnectionTimeout(20000);
        config.setConnectionTestQuery("select 1");
        config.setDriverClassName("org.sqlite.JDBC");
        config.setJdbcUrl(DBUrl);
        return new HikariDataSource(config);
    }

    public static Connection getConnect(String name) {
        String DBUrl = "jdbc:sqlite::resource:sqlite/"+name+".sqlite";
        Statement stmt;
        try {
            Class.forName("org.sqlite.JDBC");
            Connection con = null;
            con = DriverManager.getConnection(DBUrl);
            if (con != null) {
                return con;
            } else {
                System.out.println("----------------jdbc 链接失败");
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("----------------jdbc 链接报错咯。。。");
            return null;
        }

    }
    public static Connection get28Connect() {
        String DBUrl = "jdbc:mysql://10.15.0.28:3306/cloudmysql?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2b8";
        Statement stmt;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = null;
            con = DriverManager.getConnection(DBUrl,"nmggs","2zhLmcL");
            if (con != null) {
                return con;
            } else {
                System.out.println("----------------jdbc 链接失败");
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("----------------jdbc 链接报错咯。。。");
            return null;
        }

    }
    private static String getDriverClassName(String dbType) {
        if (dbType.equals("1")) {
            return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        } else if (dbType.equals("2")) {
            return "oracle.jdbc.OracleDriver";
        } else if (dbType.equals("3")) {
            return "dm.jdbc.driver.DmDriver";
        } else if (dbType.equals("4")) {
            return "com.mysql.cj.jdbc.Driver";
        } else {
            return null;
        }
    }

    private static String getTestQuery(String dbType) {
        if (dbType.equals("1")) {
            return "select 1 ";
        } else if (dbType.equals("2")) {
            return "select 1 from dual";
        } else if (dbType.equals("3")) {
            return "select 1 ";
        } else if (dbType.equals("4")) {
            return "select 1 ";
        } else {
            return null;
        }
    }

    public static List<Map<String, String>> getMapByPS(PreparedStatement ps) throws SQLException {
        List<Map<String, String>> data = new ArrayList<>();
        ps.executeQuery();
        ResultSet resultSet = ps.getResultSet();
        if (null != resultSet) {
            while (resultSet.next()) {
                ResultSetMetaData rsmd = resultSet.getMetaData();
                int columnCount = rsmd.getColumnCount();
                Map<String, String> row = new HashMap<>();
                for (int i = 1; i <= columnCount; i++) {
                    String colValue = resultSet.getString(i);
                    row.put(rsmd.getColumnName(i).toUpperCase(), colValue);
                }
                data.add(row);
            }
        }
        try {
            if (!ps.isClosed()) {
                ps.close();
            }
        } catch (Exception ignored) {
        }
        return data;
    }
    public static List<Map<String, String>> getMapByPSSqlite(PreparedStatement ps) throws SQLException {
        List<Map<String, String>> data = new ArrayList<>();
        ResultSet resultSet = ps.executeQuery();
        if (null != resultSet) {
            while (resultSet.next()) {
                ResultSetMetaData rsmd = resultSet.getMetaData();
                int columnCount = rsmd.getColumnCount();
                Map<String, String> row = new HashMap<>();
                for (int i = 1; i <= columnCount; i++) {
                    String colValue = resultSet.getString(i);
                    row.put(rsmd.getColumnName(i), colValue);
                }
                data.add(row);
            }
        }
        try {
            if (!ps.isClosed()) {
                ps.close();
            }
        } catch (Exception ignored) {
        }
        return data;
    }
}
